package com.lap.auth.domain.user.service;

import com.lap.auth.domain.user.User;
import com.lap.auth.domain.user.repository.UserRepository;
import com.lap.auth.shared.errors.UserError;
import com.lap.framework.common.exception.BizException;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class UserValidator {

  private final UserRepository userRepository;

  public void validateUserNameUnique(String userName) {
    userRepository
        .findByUserName(userName)
        .ifPresent(user -> BizException.interrupt(UserError.USER_EXISTS));
  }

  public User assertUserExists(Integer id) {
    return userRepository
        .findById(id)
        .orElseThrow(() -> BizException.newBiz(UserError.USER_NOT_FOUND));
  }
}
